package com.dlc.yunguang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import com.dlc.commonlibrary.dialog.DoResultDialog;
import com.dlc.commonlibrary.dialog.EditDialog;
import com.dlc.commonlibrary.utils.RecyclerViewUtil;
import com.dlc.commonlibrary.utils.ToastUtil;
import com.dlc.commonlibrary.widget.CommonItemView;
import com.dlc.commonlibrary.widget.TitleBar;
import com.dlc.yunguang.base.BaseActivity;
import com.dlc.yunguang.net.bean.RFIDListBean;
import com.dlc.yunguang.net.bean.SimpleBean;
import com.dlc.yunguang.net.protocol.StockProto;
import com.dlc.yunguang.net.protocol.TagProto;
import com.dlc.yunguang.R;
import com.dlc.yunguang.adapter.TagProductInfoAdapter;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.dlc.yunguang.activity.TagManageActivity.EXTRA_RFIDS;

public class BucaoScrapActivity extends BaseActivity {

    @BindView(R.id.title_bar) TitleBar mTitleBar;
    @BindView(R.id.item_rfid_amount) CommonItemView mItemRfidAmount;
    @BindView(R.id.recycler_view) RecyclerView mRecyclerView;
    @BindView(R.id.btn_scrap) Button mBtnScrap;

    private TagProductInfoAdapter<RFIDListBean.DataBean.ListBean> mAdapter;

    private ArrayList<String> mRfids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        resovleIntent();
        mTitleBar.leftExit(this);
        mItemRfidAmount.setContent(String.valueOf(mRfids.size()));

        mAdapter = new TagProductInfoAdapter<>();
        initRecyclerView();
        initFakeData();
        // TODO: 2017/7/19 临时代码，假数据
       // mAdapter.setNewData(initFakeData());
    }

    @Override
    protected int getLayoutID() {
        return R.layout.activity_bucao_scrap;
    }

    @OnClick({ R.id.btn_scrap })
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_scrap:
                showScrapDialog();
                break;
        }
    }

    private void resovleIntent() {
        Intent intent = getIntent();
        mRfids = intent.getStringArrayListExtra(EXTRA_RFIDS);
    }

    private void initRecyclerView() {
      //  mAdapter = new TagProductInfoAdapter<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        RecyclerViewUtil.setDefaultDivider(mRecyclerView, linearLayoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * todo 解绑设备原因对话框
     */
    private void showScrapDialog() {

        EditDialog dialog = EditDialog.newDialog(this)
            .setButton(R.string.sure, R.string.cancel)
            .setTitleRes(R.string.scrap_reason)
            .setHint(R.string.input_scrap_reason);

        dialog.setOnPositiveClickListener(new EditDialog.OnPositiveClickListener() {
            @Override
            public void onPositiveClick(EditDialog dialog, @NonNull String newText,
                @NonNull String oldText) {

                if (TextUtils.isEmpty(newText)) {
                    showToast(R.string.reason_not_empty);
                    return;
                }

                dialog.dismiss();
                doScrap(newText);
            }
        });

        dialog.show();
    }

    /**
     * 开始报废布草
     */
    private void doScrap(String reason) {

        final DoResultDialog doResultDialog = DoResultDialog.newDialog(this)
            .setSuccessImage(R.mipmap.ic_success)
            .setWorkingText(getString(R.string.commit_scrap_ing))
            .setSuccessText(getString(R.string.commit_scrap_success));

        doResultDialog.setCancelable(false);

        doResultDialog.showWorking();

        TagProto.get()
            .unbindRFID(reason, 2, mAdapter.getRFIDs())
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<SimpleBean>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    doResultDialog.dismiss();
                    showToast("提交失败," + e.getMessage());
                }

                @Override
                public void onNext(SimpleBean simpleBean) {
                    doResultDialog.showResultAndRun(true, new Runnable() {
                        @Override
                        public void run() {
                            doResultDialog.dismiss();
                            finish();
                        }
                    }, 1000);
                }
            });
    }

    private void initFakeData() {
        StringBuilder sb = new StringBuilder();
        for(String rfid : mRfids){
            sb.append(rfid+",");
        }
        StockProto.get()
                .query_by_rfid(sb.toString(),"1","100")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<RFIDListBean.DataBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtil.show(getActivity(), e.getMessage());
                    }

                    @Override
                    public void onNext(RFIDListBean.DataBean bean) {
                        mAdapter.setNewData(bean.list);
                    }
                });
    }
}
